/***
 * Copyright (C) 2011  naikon, wexoo
 * android@geekosphere.org
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package net.wexoo.group_cost_calculator.gui.util;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import net.wexoo.group_cost_calculator.dao.DALUtils;
import net.wexoo.group_cost_calculator.gui.HomeActivity;
import net.wexoo.group_cost_calculator.util.Log;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

/**
 * The Class UIUtils.
 * 
 * @author wexoo
 */
public class UIUtils {

   public static final String TAG = UIUtils.class.getSimpleName();

   public static final String NUMBER_FORMAT_2_DECIMAL_PLACES = "#.##";

   /**
    * Invoke "home" action, returning to {@link HomeActivity}.
    * 
    * @param context applicationcontext to st
    */
   public static void goHome(final Context context) {
      final Intent intent = new Intent(context, HomeActivity.class);
      intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
      context.startActivity(intent);
   }

   public static void showShortToast(final Context context, final String text) {
      Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
   }

   public static void showShortToast(final Context context, final int resId) {
      showShortToast(context, context.getString(resId));
   }

   public static void showLongToast(final Context context, final String text) {
      Toast.makeText(context, text, Toast.LENGTH_LONG).show();
   }

   public static void showLongToast(final Context context, final int resId) {
      showLongToast(context, context.getString(resId));
   }

   public static String formatDoubleToTwoDecimalPlacesString(Double d) {
      DecimalFormat twoDForm = new DecimalFormat(NUMBER_FORMAT_2_DECIMAL_PLACES);
      return twoDForm.format(d);
   }

   public static String getDateAsGermanString(final Date date) {
      final SimpleDateFormat format = new SimpleDateFormat(DALUtils.GERMAN_DATE_FORMAT);
      return format.format(date);
   }

   public static String getSmallDateAsString(final Date date) {
      final SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy");
      return format.format(date);
   }

   public static String getBigDateAsString(final Date date) {
      final SimpleDateFormat format = new SimpleDateFormat("dd. MMMM yyyy");
      return format.format(date);
   }

   public static Date getStringAsDate(final String dateString) {
      final SimpleDateFormat format = new SimpleDateFormat("dd. MMMM yyyy HH:mm");
      try {
         return format.parse(dateString);
      } catch (final ParseException e) {
         Log.e(UIUtils.TAG, e.getMessage());
         e.printStackTrace();
      }
      return null;
   }

   public static String addLeadingZeroToLong(final Long value) {
      return value < 10 ? "0" + value : value.toString();
   }

}
